package com.jic.member.controller.power;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.power.MevipRule;
import com.jic.member.request.power.MevipRuleRequest;
import com.jic.member.response.power.MevipRuleResponse;
import com.jic.member.service.power.MevipRuleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@ApiOperation(value = "会员vip权益关联controller")
@RestController
@RequestMapping("/mevipRule")
@Slf4j
public class MevipRuleController {

    @Autowired
    MevipRuleService mevipRule;
    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取Vip权益关联所有列表")
    @ResponseBody
    @RequestMapping("/queryVipRuleList")
    public RestResult<PageResult<MevipRuleResponse>> queryVipRuleList(@RequestBody MevipRuleRequest form) {
        log.info("开始进入vip权益关联列表=========》,{}", form.toString());
        PageResult<MevipRuleResponse> pageResult = new PageResult<>();
        try {
            Page page = new Page();
            page.setLimit(form.getLimit());
            page.setStart(form.getStart());
            MevipRule memberRequest = BeanUtil.copy(form, MevipRule.class);
            PageResult<MevipRule> result = mevipRule.queryPage(memberRequest, page);
            List<MevipRuleResponse> memberResponseList = result.getRows().stream().map(temp -> {
                MevipRuleResponse memberResponse = BeanUtil.copy(temp, MevipRuleResponse.class);
                return memberResponse;
            }).filter(item -> item != null).collect(Collectors.toList());
            pageResult.setRows(memberResponseList);
            pageResult.setTotal(result.getTotal());
        }catch (Exception e) {
            log.error("error",e.getLocalizedMessage());
        }

        return RestResult.success(pageResult);
    }
    /*
     * 分页查询
     * */
    @ApiOperation(value = "查询商家/店铺vip设置所有列表")
    @ResponseBody
    @RequestMapping("/queryMerchantOrStoreVipRuleList")
    public RestResult<PageResult<MevipRuleResponse>> queryMerchantOrStoreVipRuleList(@RequestBody MevipRuleRequest form) {
        log.info("开始进入vip权益关联列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MevipRule memberRequest = BeanUtil.copy(form, MevipRule.class);
        PageResult<MevipRule> result = mevipRule.queryMerchantOrStorePage(memberRequest, page);
        List<MevipRuleResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MevipRuleResponse memberResponse = BeanUtil.copy(temp, MevipRuleResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MevipRuleResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "查询列表不分页")
    @ResponseBody
    @RequestMapping("/queryVipRuleListOther")
    public RestResult queryVipRuleListOther(@RequestBody MevipRuleRequest form) {
        MevipRule memberRequest = BeanUtil.copy(form, MevipRule.class);
        List<MevipRule> query = mevipRule.query(memberRequest);
        return RestResult.success(query);
    }

    @ApiOperation(value = "保存方法")
    @ResponseBody
    @RequestMapping("/saveVipRule")
    public RestResult saveVipRule(@RequestBody MevipRuleRequest form){
        MevipRule memberRequest = BeanUtil.copy(form, MevipRule.class);
        mevipRule.insert(memberRequest);
        return RestResult.success("1");
    }
}
